#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   user.py
@Time    :   2024-07-09 13:04:53
@Author  :   chakcy 
@Email   :   947105045@qq.com
@description   :   用户相关接口
'''
from typing import Union

from fastapi import APIRouter, Security, Depends, Request, File, UploadFile

from ..service import UserService
from ..dependencies.redis import RedisDependency
from ..dependencies.jwt import Authorization
from ..models import CommonResponseModel
from ..models.user import (UserLoginIn, 
                           UserLoginOut,
                           AddUserIn,
                           UpdateUserIn,
                           AddUserOut)

router = APIRouter(prefix="/user", tags=["user-controller"])

@router.post("/login", 
            summary="用户登录", 
            description="用户登录", 
            response_model=Union[UserLoginOut, CommonResponseModel])
async def login(user_login:UserLoginIn, 
                redis:RedisDependency = Depends()):
    return await UserService.login(user_login, redis)

@router.post("/register",
             summary="用户注册",
             description="用户注册",
             response_model=Union[AddUserOut, CommonResponseModel])
async def register(user_data:AddUserIn):
    return await UserService.register(user_data)

@router.get("/info", 
            summary="获取用户信息", 
            description="获取用户信息",
            response_model=CommonResponseModel)
async def getUserInfo(request: Request,
                      token:str = Security(Authorization),
                      redis:RedisDependency = Depends()):
    return await UserService.getUserInfo(request=request, redis=redis)
    
@router.get("/logout", 
            summary="用户退出", 
            description="用户退出",
            response_model=CommonResponseModel)
async def logout(request: Request,
                      token:str = Security(Authorization),
                      redis:RedisDependency = Depends()):
    return await UserService.logout(request.state.data, redis)

@router.post("/update", 
             summary="更新用户信息",
             description="更新用户信息",
             response_model=CommonResponseModel)
async def update_user(user: UpdateUserIn,
                      request: Request,
                      token: str = Security(Authorization),
                      redis: RedisDependency = Depends()):
    return await UserService.update_user_info(request=request,
                                              user=user,
                                              redis=redis)

@router.post("/avatar", 
             summary="上传用户头像", 
             description="上传用户头像",
             response_model=CommonResponseModel)
async def upload_avatar(request: Request,
                        file: UploadFile = File(...),
                        redis: RedisDependency = Depends(),
                        token: str = Security(Authorization)):
    return await UserService.uploader_avatar(request=request,
                                             file=file,
                                             redis=redis)
